package core;

import ie.FieldBoard;
import ie.PlayerBoard;
import ie.Shaker;
import boundary.UI;
import controllers.GSTDGameManager;
import core.subcontrollers.Rules;
import dto.*;
import entity.Account;
import entity_controllers.Option;
import entity_controllers.OptionManager;

public class GameManager extends GSTDGameManager {
	private int currentPlayer;
	private int STARTING_BALANCE = 30000;

	@Override
	public void initialize() {
		shaker = new Shaker(Creator.createDices());
		gameBoard = new FieldBoard(Creator.createFields());
		playerBoard = new PlayerBoard(Creator.createPlayers(STARTING_BALANCE));

	}

	@Override
	public void endMatch() {
		Player winner = null;
		Player[] players = playerBoard.getPlayers();
		for (int i = 0; i < players.length; i++)
			if (!((Account) players[i].getAccount()).isBankrupt()) {
				winner = players[i];
				break;
			}
		UI.get().getMessage("Player " + winner.getName() + " won!");
	}

	@Override
	public void endRound() {}

	@Override
	public boolean isMatchDone() {
		return Rules.hasWinner(playerBoard);
	}

	@Override
	public boolean isRoundDone() {
		return true;
	}

	@Override
	public void match() {
		if (!UI.get().getBoolean("Start spil?", "Start", "Slut"))
			System.exit(0);

		// reset game
		Player[] players = playerBoard.getPlayers();
		for (int i = 0; i < players.length; i++) {
			players[i].getAccount().resetAll();
			players[i].getAccount().addBalance(STARTING_BALANCE);
			players[i].setFieldIndex(0);
		}
		Field[] fields = gameBoard.getFields();
		for (int i = 0; i < fields.length; i++) {
			fields[i].resetAll();
		}
		UI.get().showPlayers(playerBoard.getPlayers());
		UI.get().showFields(gameBoard.getFields());
		this.currentPlayer = 0;
	}

	@Override
	public void round() {}

	@Override
	public void turn() {
		System.out.println(currentPlayer);
		Player player = playerBoard.getPlayer(currentPlayer);
		OptionManager om = ((Account) player.getAccount()).getOM();

		executeEffects(player);
		while (om.hasOptions())
			startTurn(player);
		this.currentPlayer++;
		this.currentPlayer %= playerBoard.getPlayers().length;
	}

	private void executeEffects(Player player) {
		Option[] effects = ((Account) player.getAccount()).getEM().getAvailableOptions();
		for (int i = 0; i < effects.length; i++) {
			effects[i].execute(player, gameBoard, playerBoard, shaker);
		}
		// ((Account) player.getAccount()).getEM().clear();
	}

	private void startTurn(Player player) {
		int c = 0;
		Option[] optionsAvailable = ((Account) player.getAccount()).getOM().getAvailableOptions();
		String[] optionsTitles = new String[optionsAvailable.length];
		
		for (int i = 0; i < optionsAvailable.length; i++) {
			optionsTitles[i] = optionsAvailable[i].getTitle();
		}
		String action = UI.get().getBooleanMultiple(player.getName()+"'s tur, hvad vil du gøre?", optionsTitles);
		for (int i = 0; i < optionsAvailable.length; i++) {
			if(action == optionsAvailable[i].getTitle()){
				c = i;
				break;
			}
		}
		
		// execute the selected option
		optionsAvailable[c].execute(player, gameBoard, playerBoard, shaker);

	}

}
